package com.pointmi.merchantdao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;

@Transactional
public class MerchantDAOImpl implements MerchantDAO {

	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	protected final Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	public void saveOrUpdate(Merchant merchant) {
		getCurrentSession().saveOrUpdate(merchant);
	}

	@SuppressWarnings("rawtypes")
	public Merchant findMerchantById(int merchantId) {
		Query query = getCurrentSession().createQuery(
				"from station where stationId = :stationId");
		query.setParameter("stationId", merchantId);
		List list = query.list();
		return foundMerchant(list);
	}

	@SuppressWarnings("rawtypes")
	private Merchant foundMerchant(List list) {
		Merchant merchant = null;
		if (list.size() > 0) {
			merchant = (Merchant) list.get(0);
		}
		return merchant;
	}

	@SuppressWarnings("unchecked")
	public List<Merchant> findAllMerchant() {
		Query query = getCurrentSession().createQuery("from station");
		return (List<Merchant>) query.list();
	}

}
